﻿#pragma once
#ifndef SDP_H
#define SDP_H
#define TRACK_MAX_NUM 2
#include <cstdio>
#include <QByteArray>

class Sdp;
class SdpTrack
{
public:
	enum class Stream
	{
		NONE = 0,
		AUDIO,
		VIDEO,
	};

private:
	friend class Sdp;

	bool isAlive{false};

	QByteArray control;		 // 示例值 streamid=0,track=0
	QByteArray control_name; // 从control提取出来的名称，示例值 streamid,track
	int control_id = -1;	 //

	int payload{-1}; // 音视频编码的编号,示例值 96,97

	QByteArray codec;	   // 编码格式
	int timebase{-1};	   // 视频时间基/音频采样率
	int audio_channel{-1}; // 音频声道 ch_layout.nb_channels

	int as{-1}; // 建议带宽 bandwidth

	bool is_send_setup{false}; // 当前track是否已经发送过setup请求
public:
	SdpTrack() {}
	SdpTrack(bool _isAlive, int _payload) : isAlive(_isAlive), payload(_payload) {}

	bool getAliveStatus() const { return isAlive; }
	int getPayload() const { return payload; }
	const QByteArray &getControl() const { return control; }
	const int &getControlId() const { return control_id; }

	int getVideoTimebase() const { return timebase; }
	int getAudioSampleRate() const { return timebase; }
	int getAudioChannel() const { return audio_channel; }
	void print() const;
};

class Sdp
{
public:
	explicit Sdp() {}
	~Sdp() {}

	int parse(const QByteArray &buf);
	SdpTrack *popTrack();
	void clear();
	bool isEmpty() const;

	SdpTrack tracks[TRACK_MAX_NUM];

	QByteArray spsBase64; // 视频编码参数
	QByteArray ppsBase64; // 视频编码参数
	QByteArray getVideoCodeccparExtradata() const;

	int audioStreamId{-1};
	int videoStreamId{-1};

	uint8_t videoPayloadType{0};
	uint8_t audioPayloadType{0};
};

#endif // SDP_H
